#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include<sys/types.h>
#include <fcntl.h>
#include <time.h>
#include <sys/mman.h>


int main(int argc,char* argv[])
{
    //准备有且只有两个元素的描述符号数组
    int fds[2];

    //把数组变成管道
    pipe(fds);

    //创建子进程
    if (fork())
    {
        char buff[256];
        int n = 0;
        int r;
        //使用管道
        while (1)
        {
            printf("父进程读管道");

            r = read(fds[0],buff,255);
            if (r>0)
            {
                buff[r]=0; //添加结束符号
                n++;
                printf("%d:%s\n",n,buff);
                if (0==strcmp(buff,"123456"))
                {
                    break;
                }
            }
        }
    }else
    {
        char buff[256];
        while (1)
        {
            bzero(buff,256);//清空数组
            scanf("%s",buff);
            write(fds[1],buff,strlen(buff));
            if (0==strcmp(buff,"123456"))
            {
                break;
            }
        }
        printf("子进程结束\n");

    }


    printf("关闭管道\n");
    //关闭管道
    close(fds[0]);
    close(fds[1]);
    printf("父进程结束\n");

    return 0;

}